<template>
  <div class="save-quick-filter-0 ">
    <div class="save-quick-filter-0__wrapper flex justify-between items-center">
      <span class="save-quick-filter-0-0__wrapper--text">{{$t('custom.quickFilter')}}</span>
      <el-tooltip :open-delay="1000" :content="$t('custom.scfsa')"
                  placement="left"
                  v-model="tooltipVisible">
        <img src="@/assets/img/save.png"
             class="cursor-pointer"
             @click="handleOpen('save')">
      </el-tooltip>
    </div>
    <el-dialog :visible.sync="dialogVisible"
               width="395px"
               custom-class="dialog__header--shadow"
               append-to-body
               ref="quick-filter-dialog"
               :title="$t('custom.quickFilter')">
      <QuickFilterAddForm :filter="filter"
                          :order="order"
                          :integratin="integratin"
                          :type="type"
                          @success="handleSuccess"
                          v-if="dialogType" />
    </el-dialog>
  </div>
</template>

<script>
import ThrottleButton from '@/components/throttle-button'
import ButtonVessel from '@/components/button-vessel'
import QuickFilterAddForm from './quick-filter-add-form.vue'
export default {
  name: 'SaveQuickFilter',
  components: {
    ThrottleButton,
    ButtonVessel,
    QuickFilterAddForm
  },
  props: {
    filter: {
      type: Object,
      default: () => ({})
    },
    order: {
      type: Object,
      default: () => ({})
    },
    integratin: {
      type: Array,
      default: () => ([])
    },
    type: {
      type: String,
      require: true
    }
  },
  data () {
    return {
      dialogType: undefined,
      tooltipVisible: false
    }
  },
  computed: {
    dialogVisible: {
      get () {
        return !!this.dialogType
      },
      set (val) {
        if (val) {
          this.dialogType = this.dialogType
        }
        this.dialogType = undefined
      }
    }
  },
  created () {
    this.$root.eventBus.$on('bus:open', this.handleOpen)
    this.$root.eventBus.$on('bus:add', () => {
      this.tooltipVisible = true
      setTimeout(() => { this.tooltipVisible = false }, 1500)
    })
  },
  activated () {
    this.$root.eventBus.$on('bus:open', this.handleOpen)
    this.$root.eventBus.$on('bus:add', () => {
      this.tooltipVisible = true
      setTimeout(() => { this.tooltipVisible = false }, 1500)
    })
  },

  // 防止keepalive 放置重复监听
  deactivated () {
    this.$root.eventBus.$off('bus:open')
    this.$root.eventBus.$off('bus:add')
  },

  beforeDestroy () {
    this.$root.eventBus.$off('bus:open')
    this.$root.eventBus.$off('bus:add')
  },

  methods: {
    handleOpen (command) {
      this.dialogType = command
      this.$emit('event:open', command)
    },
    handleClose () {
      this.dialogType = undefined
      this.$emit('event:close')
      this.$root.eventBus.$emit('bus:close')
    },
    handleSuccess () {
      this.handleClose()
    }
  }

}
</script>
<style lang="scss">
.save-quick-filter-0{
  width: 100%;
  padding: 16px;
  margin-bottom: 8px;
  &__wrapper {
    height: 48px;
    box-shadow: 0px 1px 0px 0px #f0f0f0;

    &--text {
      font-size: 14px;
      color: #404040;
      line-height: 14px;
      font-weight: 600;
    }
    .el-icon-circle-plus-outline {
      font-size: 16px;
      color: #878787;
    }
  }
  &__menu {
    background: #ffffff;
    box-shadow: 0px 0px 8px 0px #f0f0f0;
    border-radius: 4px;
    border: 1px solid #f0f0f0;
    padding: 8px 0;
    margin-top: 6px !important;
    & > .popper__arrow {
      display: none;
    }
    .el-dropdown-menu__item {
      font-size: 14px;
      color: #404040;
      line-height: 34px;
    }
  }
}
</style>
